package com.bayi.program;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * @author Administrator on 2025-05-14.
 * <p>
 * 站在顶峰 看世界
 * 落到谷底 思人生
 */
public class DateUtils {

    public static String format = "yyyy-MM-dd HH:mm:ss";
    public static String format1 = "yyyy-MM-dd";

    /**
     * 日期字符串转换Date实体
     *
     * @param serverTime
     * @param format
     * @return
     */
    public static Date parseServerTime(String serverTime, String format) {
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINESE);
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        Date date = null;
        try {
            date = sdf.parse(serverTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }


    public static String getCurrentTime() {

        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINESE);
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));

        return sdf.format(new Date());
    }

    public static String getCurrentTime2() {

        SimpleDateFormat sdf = new SimpleDateFormat(format1, Locale.CHINESE);

        return sdf.format(new Date());
    }

    /**
     * 秒数转换成时分秒
     *
     * @param lSeconds
     * @return
     */
    public static String convertSecToTimeString(long lSeconds) {
        long nHour = lSeconds / 3600;
        long nMin = lSeconds % 3600;
        long nSec = nMin % 60;
        nMin = nMin / 60;

        return String.format("%02d小时%02d分钟%02d秒", nHour, nMin, nSec);
    }


    /**
     * 加减时间获取视频追溯时间
     *
     * @param lSeconds
     * @param isAdd    true:加时间； false:减时间
     * @return
     */
    public static String additionSubtraction(long lSeconds, long interval, boolean isAdd) {
        long time;
        if (isAdd) {
            time = lSeconds + interval;
        } else {
            time = lSeconds - interval;
        }
        String date = timeStamp2Date(time, DateUtils.format);
        Date dsTime = DateUtils.format(date, DateUtils.format);
        return DateUtils.format(dsTime, "yyyyMMdd't'HHmmss'00z'");
    }

    /**
     * @param time
     * @param format
     * @return
     */
    public static String timeStamp2Date(long time, String format) {
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(time));
    }

    public static String format(Date date, String pattern) {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        return df.format(date);
    }

    public static Date format(String date, String pattern) {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        try {
            return df.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new Date();
    }


    /**
     * 得到当前时间string类型
     */
    public static String getStrTime() {
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);
        String time = null;
        try {
            time = sdf.format(new Date());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return time;
    }


    /**
     * 按照格式转换成对应的string
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String parseToDate(Date date, String pattern) {
        SimpleDateFormat formater = new SimpleDateFormat(pattern, Locale.CHINA);
        try {
            return formater.format(date);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String formatTimestamp(long timestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.getDefault());
        Date date = new Date(timestamp);
        return sdf.format(date);
    }

    /**
     * 得到当前时间Date类型
     */
    public static Date getTime() {
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);
        Date time = null;
        try {
            time = sdf.parse(sdf.format(new Date()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }

    public static boolean areSameDay(Date date) {
        Calendar calDateA = Calendar.getInstance();
        calDateA.setTime(date);
        Calendar calDateB = Calendar.getInstance();
        Date now = new Date();
        calDateB.setTime(now);

        return calDateA.get(Calendar.YEAR) == calDateB.get(Calendar.YEAR)
                && calDateA.get(Calendar.MONTH) == calDateB.get(Calendar.MONTH)
                && calDateA.get(Calendar.DAY_OF_MONTH) == calDateB.get(Calendar.DAY_OF_MONTH);
    }


    /**
     * 获取今天是周几
     *
     * @return
     */
    public static int getDayForWeek() {
        Date today = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(today);
        int dayForWeek = 0;
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            dayForWeek = 7;
        } else {
            dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
        }
        return dayForWeek;
    }


    /**
     * 得到时间
     *
     * @param hourOfDay
     * @param minute
     * @return
     */
    public static long getTimeInMillis(int hourOfDay, int minute) {
        Calendar mCalendar = Calendar.getInstance();
        mCalendar.setTimeInMillis(System.currentTimeMillis());
        mCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
        mCalendar.set(Calendar.MINUTE, minute);
        mCalendar.set(Calendar.SECOND, 0);
        mCalendar.set(Calendar.MILLISECOND, 0);
        return mCalendar.getTimeInMillis();
    }

    public static String formatTimes(long timestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
        Date date = new Date(timestamp);
        return sdf.format(date);
    }
}